$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'xinpai/users/list',
        datatype: "json",
        colModel: [			
			{ label: '用户ID', name: 'uid', index: 'u_id', width: 50, key: true },
		/*	{ label: '设备imei号', name: 'imei', index: 'imei', width: 80 }, */
			{ label: '姓名', name: 'name', index: 'name', width: 80 },
            { label: '所属项目', name: 'projectId', index: 'project_id', width: 80 ,formatter:function (value){
                for (var i = 0; i < vm.projectList.length; i++) {
                    var obj = vm.projectList[i];
                    if(obj.pid==parseInt(value)){
                        return obj.projectName;
                    }
                }
            }},
            { label: '所属部门', name: 'deptId', index: 'dept_id', width: 80,formatter:function (value) {
                for (var i = 0; i < vm.dict.deptId.length; i++) {
                    var obj = vm.dict.deptId[i];
                    if(obj.code==value){
                        return `<span class="label label-success">${obj.value}</span>`;
                    }
                }
            } },
            { label: '职位', name: 'positionId', index: 'position_id', width: 80 },
            { label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
 /*           { label: '性别', name: 'sex', index: 'sex', width: 80 },
			{ label: '生日', name: 'birthday', index: 'birthday', width: 80 }, 			
			{ label: '身份证号', name: 'idCard', index: 'id_card', width: 80 },
			{ label: '民族', name: 'volk', index: 'volk', width: 80 }, 			
			{ label: '籍贯', name: 'naivePlace', index: 'naive_place', width: 80 }, 			
			{ label: '地址', name: 'address', index: 'address', width: 80 }, 			
			{ label: '婚姻状况', name: 'marriage', index: 'marriage', width: 80 },
			{ label: '所属项目', name: 'projectId', index: 'project_id', width: 80 }, 			
			{ label: '人员状态', name: 'state', index: 'state', width: 80 }		*/
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
function isPhone(str) {
    return /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9]|19[0-9]|17[0-9]|16[0-9])\d{8}$/.test(str);
}
var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            username: null,
        },
		showList: true,
		title: null,
		users: {areaList:[]},
        projectList:[],
        itemList:[],
        dict:{deptId:[],marriage:[],sex:[]}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.users = {areaList:[],deptId:0,marriage:0,sex:0,projectId:0};
		},
		update: function (event) {
			var uId = getSelectedRow();
			if(uId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(uId)
		},
		saveOrUpdate: function (event) {
			var url = vm.users.uid == null ? "xinpai/users/save" : "xinpai/users/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.users),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var uIds = getSelectedRows();
			if(uIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "xinpai/users/delete",
                    contentType: "application/json",
				    data: JSON.stringify(uIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(uId){
			$.get(baseURL + "xinpai/users/info/"+uId, function(r){
                vm.users = r.users;
                vm.selectInfo();
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            var postData=null;
            if(vm.q.username!=null&&vm.q.username!=""){
                postData={}
                if(isPhone(vm.q.username)){
                    postData.mobile=vm.q.username
                }else{
                    postData.name=vm.q.username
                }
            }
            console.log(postData);
            $("#jqGrid").jqGrid('setGridParam',{
                postData,
                page:page
            }).trigger("reloadGrid");
		},
        selectProjectList:function (data={parentId:0,type:0}) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/project/list',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                        if(data.parentId>0){
                            vm.itemList=r.list;
                        }else{
                            vm.projectList=r.list;
                        }

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        selectInfo:function () {
            if(this.users.projectId>0){
                this.selectProjectList({parentId:this.users.projectId,type:1});
            }

        },
        selectDictList:function (name,type) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'sys/dict/select',
                contentType: "application/json",
                data: {name},
                success: function(r){
                    if(r.code === 0){
                        for (let key in vm.dict) {
                            if(key==type){
                                vm.dict[key]=r.list;
                            }
                        }
                    }else{
                        alert(r.msg);
                    }
                }
            });
        }
	},
    created:function () {
        this.selectProjectList();
        this.selectDictList("部门",'deptId')
        this.selectDictList("婚姻状况",'marriage')
        this.selectDictList("性别",'sex')
    }
});